package view;

  
import java.awt.Color;
import model.Account;
import view.AbstractAccountView;
import java.util.*;
import java.text.NumberFormat;
import javax.swing.table.DefaultTableModel;
import model.Share;



/**
 *
 * @author  diego
 */
public class AccountView extends AbstractAccountView {

    private NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US );
    private double openingMoney = 0.0; 
    private double moneyBalance = 0.0; 
    private Share share;
    /** Creates new form AccountView */
    public AccountView(Account account, Share share) {
        super( account );
        setShare(share);
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfOpeningMoney = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfMoneyBalance = new javax.swing.JTextField();
        jtbExtract = new javax.swing.JTabbedPane();
        jspShareBalance = new javax.swing.JScrollPane();
        jtShareBalance = new javax.swing.JTable();
        jspPeriodReview = new javax.swing.JScrollPane();
        jtPeriodReview = new javax.swing.JTable();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(expecon.ExpEconApp.class).getContext().getResourceMap(AccountView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("borderAccount.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("Form.border.titleFont"), new java.awt.Color(113, 196, 243))); // NOI18N
        setPreferredSize(new java.awt.Dimension(1000, 150));

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel1.setText(resourceMap.getString("jlOpenMoney.text")); // NOI18N

        jtfOpeningMoney.setColumns(10);
        jtfOpeningMoney.setEditable(false);
        jtfOpeningMoney.setText(moneyFormat.format( getAccount().getOpeningMoney() ));

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel3.setText(resourceMap.getString("jlTotalBalance.text")); // NOI18N

        jtfMoneyBalance.setColumns(10);
        jtfMoneyBalance.setEditable(false);
        jtfMoneyBalance.setText(moneyFormat.format( getAccount().getMoneyBalance() ));

        jtbExtract.setBackground(new java.awt.Color(255, 255, 255));
        jtbExtract.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        jtbExtract.setFont(new java.awt.Font("DejaVu Sans", 1, 11));

        jspShareBalance.setBackground(new java.awt.Color(255, 255, 255));

        jtShareBalance.setAutoCreateRowSorter(true);
        jtShareBalance.setModel(new DefaultTableModel(getAccount().getExtractData(), getAccount().getExtractHeader()));
        jtShareBalance.setEnabled(false);
        jtShareBalance.setFillsViewportHeight(true);
        jtShareBalance.setGridColor(new java.awt.Color(113,196,243));
        jspShareBalance.setViewportView(jtShareBalance);

        jtbExtract.addTab(resourceMap.getString("jspjtPocket.TabConstraints.tabTitle"), jspShareBalance); // NOI18N

        jspPeriodReview.setBackground(new java.awt.Color(255, 255, 255));

        jtPeriodReview.setModel(new DefaultTableModel(getShare().getHistoryData(), getShare().getHistoryHeader()));
        jtPeriodReview.setFillsViewportHeight(true);
        jtPeriodReview.setGridColor(new java.awt.Color(113,196,243));
        jtPeriodReview.setRowSelectionAllowed(false);
        jtPeriodReview.setSelectionBackground(new java.awt.Color(153, 255, 153));
        jspPeriodReview.setViewportView(jtPeriodReview);

        jtbExtract.addTab(resourceMap.getString("jspjtPeriodReview.TabConstraints.tabTitle"), jspPeriodReview); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfOpeningMoney, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfMoneyBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtbExtract, javax.swing.GroupLayout.DEFAULT_SIZE, 671, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jtbExtract, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfOpeningMoney, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfMoneyBalance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(49, Short.MAX_VALUE))
        );

        jtbExtract.getAccessibleContext().setAccessibleName(resourceMap.getString("jtpPocket.AccessibleContext.accessibleName")); // NOI18N
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jspPeriodReview;
    private javax.swing.JScrollPane jspShareBalance;
    private javax.swing.JTable jtPeriodReview;
    private javax.swing.JTable jtShareBalance;
    private javax.swing.JTabbedPane jtbExtract;
    private javax.swing.JTextField jtfMoneyBalance;
    private javax.swing.JTextField jtfOpeningMoney;
    // End of variables declaration//GEN-END:variables
    
    
    public void setShare(Share share) {
        this.share = share;
    }
 
     
    public Share getShare() {
        return share;
    }
    
    @Override
    public void updateDisplay() {
        jtShareBalance.setModel(new DefaultTableModel(getAccount().getExtractData(), getAccount().getExtractHeader()));
        jtPeriodReview.setModel(new DefaultTableModel(getShare().getHistoryData(), getShare().getHistoryHeader()));
        
        openingMoney = getAccount().getOpeningMoney();
        moneyBalance = getAccount().getMoneyBalance();
        if(openingMoney < 0  ){
            jtfOpeningMoney.setText( moneyFormat.format( openingMoney ) );
            jtfOpeningMoney.setBackground(new Color(225,0,0)); 
        }else{
            jtfOpeningMoney.setText( moneyFormat.format( openingMoney ) );
            jtfOpeningMoney.setBackground(new Color(225,225,225)); 
        }
        if( moneyBalance < 0){
            jtfMoneyBalance.setText( moneyFormat.format( moneyBalance ) );
            jtfMoneyBalance.setBackground(new Color(225,0,0));
        }else{  
            jtfMoneyBalance.setText( moneyFormat.format( moneyBalance ) );
            jtfMoneyBalance.setBackground(new Color(225,225,225));
        }
  }
}
